package cn.ifafu.ifafu.service.parser

import cn.ifafu.ifafu.bean.dto.IFResponse
import java.util.regex.Pattern

class ChangePasswordParser : BaseParser<IFResponse<Unit>>() {

    private val regex = "<script language='javascript'>alert\\('.*'\\);</script>".toRegex()

    override fun parse(html: String): IFResponse<Unit> {
        check(html)
        val matcherResult = regex.find(html)
            ?: return IFResponse.failure("密码修改失败")
        var alert = matcherResult.value
        alert = alert.substring(37, alert.length - 12)
        return if (alert.contains("成功")) {
            IFResponse.success(Unit)
        } else {
            IFResponse.failure(alert)
        }
    }
}